package com.astn.bks;

import java.util.ArrayList;
import java.util.Currency;
import java.util.HashMap;
import java.util.Map;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.astn.adapter.booking.DestinationAdapter;
import com.astn.adapter.booking.ProviderAdapter;
import com.astn.bks.util.BksConstants;
import com.astn.bks.util.WebserviceUtils;
import com.astn.entity.booking.QuoteTown;
import com.astn.entity.booking.QuoteUser;

public class Home extends Activity {

	// private ViewFlipper homeFlipper;
	private ListView lsvTop;
	private Button btnNext;
	private Button btnBack;
	private TextView txvPage;
	private Button btnTopProvider;
	private Button btnTopDestination;
	private ArrayList<QuoteUser> lstUser;// Top provider
	private ArrayList<QuoteTown> lstTown;// Top destination
	private Intent providerService;
	private int topProviderCurrentPage = 0;
	private int totalProviderPage = 0;
	private int topDestinationCurrentPage = 0;
	private int totalDestinationPage = 0;
	private int topMode = BksConstants.TOP_PROVIDER;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.home);
		lsvTop = (ListView) findViewById(R.id.lsvTop);
		btnNext = (Button) findViewById(R.id.btnNextPage);
		btnBack = (Button) findViewById(R.id.btnPreviousPage);
		txvPage = (TextView) findViewById(R.id.txvPage);
		btnTopProvider = (Button) findViewById(R.id.btnTopProvider);
		btnTopDestination = (Button) findViewById(R.id.btnTopDestination);
		getNumberPageProvider();
		txvPage.setText(String.valueOf(topProviderCurrentPage + 1) + "/"
				+ String.valueOf(totalProviderPage));
		reloadTopProvider();
		btnNext.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (topMode == BksConstants.TOP_PROVIDER) {
					getNumberPageProvider();
					if (topProviderCurrentPage < totalProviderPage - 1) {
						topProviderCurrentPage++;
					} else {
						topProviderCurrentPage = 0;
					}
					txvPage.setText(String.valueOf(topProviderCurrentPage + 1)
							+ "/" + String.valueOf(totalProviderPage));
					reloadTopProvider();
				} else {
					getNumberPageDestination();
					if (topDestinationCurrentPage < totalDestinationPage - 1) {
						topDestinationCurrentPage++;
					} else {
						topDestinationCurrentPage = 0;
					}
					txvPage.setText(String
							.valueOf(topDestinationCurrentPage + 1)
							+ "/"
							+ String.valueOf(totalDestinationPage));
					reloadTopDestination();
				}
			}

		});
		btnBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (topMode == BksConstants.TOP_PROVIDER) {
					getNumberPageProvider();
					if (topProviderCurrentPage > 0) {
						topProviderCurrentPage--;
					} else {
						topProviderCurrentPage = totalProviderPage - 1;
					}
					txvPage.setText(String.valueOf(topProviderCurrentPage + 1)
							+ "/" + String.valueOf(totalProviderPage));
					reloadTopProvider();
				} else {
					getNumberPageDestination();
					if (topDestinationCurrentPage > 0) {
						topDestinationCurrentPage--;
					} else {
						topDestinationCurrentPage = totalDestinationPage - 1;
					}
					txvPage.setText(String
							.valueOf(topDestinationCurrentPage + 1)
							+ "/"
							+ String.valueOf(totalDestinationPage));
					reloadTopDestination();

				}
			}

		});
		btnTopProvider.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				topMode = BksConstants.TOP_PROVIDER;
				getNumberPageProvider();
				txvPage.setText(String.valueOf(topProviderCurrentPage + 1)
						+ "/" + String.valueOf(totalProviderPage));
				reloadTopProvider();
				btnTopProvider.setBackgroundResource(R.color.selected);
				btnTopDestination.setBackgroundResource(R.color.not_selected);
			}

		});
		btnTopDestination.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				topMode = BksConstants.TOP_DESTINATION;
				getNumberPageDestination();
				txvPage.setText(String.valueOf(topDestinationCurrentPage + 1)
						+ "/" + String.valueOf(totalDestinationPage));
				reloadTopDestination();
				btnTopProvider.setBackgroundResource(R.color.not_selected);
				btnTopDestination.setBackgroundResource(R.color.selected);
			}

		});

	}

	private void reloadTopProvider() {
		lstUser = new ArrayList<QuoteUser>();
		Object resultObj = null;
		Map<String, Object> mapPara = new HashMap<String, Object>();
		mapPara.put("PAGE", topProviderCurrentPage);
		mapPara.put("PAGESIZE", BksConstants.PAGESIZE);
		try {
			resultObj = WebserviceUtils.call("com_xmlrpc", "getTopProvider",
					mapPara);
		} catch (Exception e) {

		}
		if (resultObj != null) {
			JSONArray arr = null;
			try {
				arr = (JSONArray) JSONValue.parse((String) resultObj);
			} catch (Exception e) {
				e.printStackTrace();
			}
			if (arr != null) {
				for (int i = 0; i < arr.size(); i++) {
					JSONObject obj = (JSONObject) arr.get(i);
					try {
						QuoteUser tmpUser = new QuoteUser();
						tmpUser.setAddress((String) obj.get("address"));
						tmpUser.setEmail((String) obj.get("email"));
						tmpUser.setId(Integer.parseInt((String) obj.get("id")));
						tmpUser.setName((String) obj.get("name"));
						tmpUser.setPhone((String) obj.get("phone"));
						tmpUser.setType(Integer.parseInt((String) obj
								.get("type")));
						lstUser.add(tmpUser);
					} catch (Exception e) {
						// TODO: handle exception
					}
				}
			}
		}
		ProviderAdapter providerAdap = new ProviderAdapter(
				getApplicationContext(), lstUser);
		lsvTop.setAdapter(providerAdap);

	}

	private void getNumberPageProvider() {
		Object resultObj = null;
		Map<String, Object> mapPara = new HashMap<String, Object>();
		mapPara.put("PAGESIZE", BksConstants.PAGESIZE);
		try {
			resultObj = WebserviceUtils.call("com_xmlrpc",
					"getPageNumberTopProvider", mapPara);
			String temp = (String) resultObj;
			Log.d("D", temp);
		} catch (Exception e) {
			// TODO: handle exception
		}
		if (resultObj != null) {
			Float f = Float.parseFloat(resultObj.toString());
			totalProviderPage = f.intValue();
		} else {
			totalProviderPage = 0;
		}
	}

	private void getNumberPageDestination() {
		totalDestinationPage = 2;
	}

	private void reloadTopDestination() {
		lstTown = new ArrayList<QuoteTown>();
		QuoteTown t1 = new QuoteTown();
		t1.setCitycode("001");
		t1.setCityname("Ha Noi");
		t1.setCitynumber("001");
		t1.setCountryid(9);
		t1.setId(1);
		t1.setRegionid(0);
		t1.setStateid(5);
		lstTown.add(t1);
		QuoteTown t2 = new QuoteTown();
		t2.setCitycode("002");
		t2.setCityname("HCM");
		t1.setCitynumber("002");
		t1.setCountryid(9);
		t1.setId(2);
		t1.setRegionid(0);
		t1.setStateid(6);
		lstTown.add(t2);
		DestinationAdapter adap = new DestinationAdapter(
				getApplicationContext(), lstTown);
		lsvTop.setAdapter(adap);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.optmenucustomer, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.mnPostRequest:
			// Select post request
			cPostRequest();
			return true;
		case R.id.mnHistory:
			// View history
			cHistory();
			return true;
		case R.id.mnMessage:
			// Message
			cMessage();
			return true;
		case R.id.mnSearch:
			// Search
			cSearch();
			return true;
		case R.id.mnPayment:
			// Payment infomation
			cPayment();
			return true;
		case R.id.mnHelp:
			// Help
			cHelp();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private void cPostRequest() {
		Intent t = new Intent(Home.this, PostRequest.class);
		startActivityForResult(t, BksConstants.POST_REQUEST);
	}

	private void cHistory() {

	}

	private void cMessage() {

	}

	private void cSearch() {

	}

	private void cPayment() {

	}

	private void cHelp() {

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
	}

	// @Override
	// public boolean onTouch(View v, MotionEvent event) {
	// switch (event.getAction()) {
	// case MotionEvent.ACTION_DOWN:
	// touchDown = event.getX();
	// break;
	// case MotionEvent.ACTION_UP:
	// touchUp = event.getX();
	// if (touchUp < touchDown) {
	// homeFlipper.setAnimation(AnimationUtils.loadAnimation(
	// Home.this, android.R.anim.slide_out_right));
	//
	// homeFlipper.showPrevious();
	// }
	// if (touchUp > touchDown) {
	// homeFlipper.setAnimation(AnimationUtils.loadAnimation(
	// Home.this, android.R.anim.slide_in_left));
	// homeFlipper.showNext();
	// }
	// break;
	//
	// default:
	// break;
	// }
	// return true;
	// }

}
